﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using Abp;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Extensions;
using Abp.Timing;
using AbpDemo.Domain.Goods;

namespace AbpDemo.Domain.Orders
{
    [Table("Order")]
    // ReSharper disable once InconsistentNaming
    public class Order_1 : FullAuditedEntity<Guid>
    {
        protected Order_1()
        {
        }

        #region Persistence Object

        /// <summary>
        /// 订单编号
        /// </summary>
        [Required]
        [StringLength(32)]
        [Column(Order = 1, TypeName = "varchar(32)")]
        public string OrderNo { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        [Required]
        public OrderState State { get; set; }

        /// <summary>
        /// 订单地址
        /// </summary>
        [MaxLength(512)]
        [Column("Address")]
        public string OrderAddress { get; set; }
        //TODO:DB迁移测试  
/*
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
*/

        [ForeignKey("OrderId")]
        public virtual ICollection<OrderItem_1> Items { get; internal set; }

        #endregion

        public static string AssignOrderNo()
        {
            Clock.Provider = ClockProviders.Utc;
            return $"{Clock.Now:yyyyMMddHHmmssfff}{new Random().Next(1, 999).ToString().PadLeft(3, '0')}";
        }

        public static Order_1 CreateInstance()
        {
            return new Order_1();
        }

        /// <summary>
        /// 下订单
        /// </summary>
        public void PlaceOrder(Goods_1 goods, int amount)
        {
            this.OrderNo = AssignOrderNo();
            this.State = OrderState.Editing;
            this.Items = new List<OrderItem_1>();
            Items.Add(new OrderItem_1
            {
                OrderId = this.Id,
                GoodsId = goods.Id,
                Amount = amount,
                SumAmount = amount * goods.Price
            });
        }

        /// <summary>
        /// 添加修改订单项
        /// </summary>
        /// <param name="goods"></param>
        /// <param name="amount"></param>
        public void AttachItems(Goods_1 goods, int amount)
        {
            if (State != OrderState.Editing)
            {
                throw new AbpException("订单状态不正确");
            }

            var existed = Items.FirstOrDefault(it => it.GoodsId == goods.Id);
            if (existed != null)
            {
                if (amount <= 0)
                {
                    Items.Remove(existed);
                }
                else
                {
                    existed.Amount = amount;
                    existed.SumAmount = existed.Amount * goods.Price;
                }
            }
            else
            {
                if (amount > 0)
                {
                    Items.Add(new OrderItem_1()
                    {
                        OrderId = this.Id,
                        GoodsId = goods.Id,
                        Amount = amount,
                        SumAmount = amount * goods.Price
                    });
                }
            }
        }

        /// <summary>
        /// 选择或修改地址
        /// </summary>
        /// <param name="address"></param>
        public void ChangeAddress(string address)
        {
            this.OrderAddress = address;
        }

        /// <summary>
        /// 提交订单
        /// </summary>
        public void SubmitOrder()
        {
            if (State != OrderState.Editing)
            {
                throw new AbpException("订单状态不正确");
            }

            State = OrderState.Confirmed;
        }
    }
}